//common_citycode
var g_all_dists=[
{provinceName:'上海',provinceCode:'SH',cityName:'上海',cityCode:'SH',dist:'上海'},
{provinceName:'北京',provinceCode:'BJ',cityName:'北京',cityCode:'BJ',dist:'北京'},
{provinceName:'广东',provinceCode:'GD',cityName:'深圳',cityCode:'SZ',dist:'深圳'},
{provinceName:'广东',provinceCode:'GD',cityName:'广州',cityCode:'GZ',dist:'广州'},
{provinceName:'广东',provinceCode:'GD',cityName:'东莞',cityCode:'DG',dist:'凤岗'},
{provinceName:'广东',provinceCode:'GD',cityName:'东莞',cityCode:'DG',dist:'厚街'},
{provinceName:'广东',provinceCode:'GD',cityName:'东莞',cityCode:'DG',dist:'城区'},
{provinceName:'广东',provinceCode:'GD',cityName:'东莞',cityCode:'DG',dist:'常平'},
{provinceName:'广东',provinceCode:'GD',cityName:'东莞',cityCode:'DG',dist:'清溪'},
{provinceName:'广东',provinceCode:'GD',cityName:'东莞',cityCode:'DG',dist:'石龙'},
{provinceName:'广东',provinceCode:'GD',cityName:'东莞',cityCode:'DG',dist:'虎门'},
{provinceName:'广东',provinceCode:'GD',cityName:'佛山',cityCode:'FS',dist:'三水'},
{provinceName:'广东',provinceCode:'GD',cityName:'佛山',cityCode:'FS',dist:'乐从'},
{provinceName:'广东',provinceCode:'GD',cityName:'佛山',cityCode:'FS',dist:'勒流'},
{provinceName:'广东',provinceCode:'GD',cityName:'佛山',cityCode:'FS',dist:'容桂'},
{provinceName:'广东',provinceCode:'GD',cityName:'佛山',cityCode:'FS',dist:'杏坛'},
{provinceName:'广东',provinceCode:'GD',cityName:'佛山',cityCode:'FS',dist:'桂城'},
{provinceName:'广东',provinceCode:'GD',cityName:'佛山',cityCode:'FS',dist:'禅城'},
{provinceName:'广东',provinceCode:'GD',cityName:'佛山',cityCode:'FS',dist:'罗村'},
{provinceName:'广东',provinceCode:'GD',cityName:'佛山',cityCode:'FS',dist:'西樵'},
{provinceName:'广东',provinceCode:'GD',cityName:'佛山',cityCode:'FS',dist:'顺德'},
{provinceName:'广东',provinceCode:'GD',cityName:'佛山',cityCode:'FS',dist:'高明'},
{provinceName:'广东',provinceCode:'GD',cityName:'惠州',cityCode:'HZ',dist:'惠城'},
{provinceName:'广东',provinceCode:'GD',cityName:'惠州',cityCode:'HZ',dist:'惠阳'},
{provinceName:'广东',provinceCode:'GD',cityName:'江门',cityCode:'JM',dist:'台山'},
{provinceName:'广东',provinceCode:'GD',cityName:'江门',cityCode:'JM',dist:'市区'},
{provinceName:'广东',provinceCode:'GD',cityName:'江门',cityCode:'JM',dist:'开平'},
{provinceName:'广东',provinceCode:'GD',cityName:'江门',cityCode:'JM',dist:'恩平'},
{provinceName:'广东',provinceCode:'GD',cityName:'江门',cityCode:'JM',dist:'新会'},
{provinceName:'广东',provinceCode:'GD',cityName:'江门',cityCode:'JM',dist:'鹤山'},
{provinceName:'广东',provinceCode:'GD',cityName:'肇庆',cityCode:'ZQ',dist:'四会'},
{provinceName:'广东',provinceCode:'GD',cityName:'肇庆',cityCode:'ZQ',dist:'市区'},
{provinceName:'广东',provinceCode:'GD',cityName:'清远',cityCode:'QY',dist:'清远'},
{provinceName:'广东',provinceCode:'GD',cityName:'湛江',cityCode:'ZJ',dist:'湛江'},
{provinceName:'广东',provinceCode:'GD',cityName:'茂名',cityCode:'MM',dist:'茂名'},
{provinceName:'广东',provinceCode:'GD',cityName:'汕头',cityCode:'ST',dist:'汕头'},
{provinceName:'广东',provinceCode:'GD',cityName:'珠海',cityCode:'ZH',dist:'珠海'},
{provinceName:'广东',provinceCode:'GD',cityName:'中山',cityCode:'ZS',dist:'中山'},
{provinceName:'广东',provinceCode:'GD',cityName:'揭阳',cityCode:'JY',dist:'揭阳'},
{provinceName:'广东',provinceCode:'GD',cityName:'韶关',cityCode:'SG',dist:'韶关'},
{provinceName:'江苏',provinceCode:'JS',cityName:'南京',cityCode:'NJ',dist:'南京市区'},
{provinceName:'江苏',provinceCode:'JS',cityName:'南京',cityCode:'NJ',dist:'江宁'},
{provinceName:'江苏',provinceCode:'JS',cityName:'南京',cityCode:'NJ',dist:'溧水'},
{provinceName:'江苏',provinceCode:'JS',cityName:'南京',cityCode:'NJ',dist:'浦口'},
{provinceName:'江苏',provinceCode:'JS',cityName:'南京',cityCode:'NJ',dist:'六合'},
{provinceName:'江苏',provinceCode:'JS',cityName:'苏州',cityCode:'SU',dist:'苏州市区'},
{provinceName:'江苏',provinceCode:'JS',cityName:'苏州',cityCode:'SU',dist:'吴江'},
{provinceName:'江苏',provinceCode:'JS',cityName:'苏州',cityCode:'SU',dist:'张家港'},
{provinceName:'江苏',provinceCode:'JS',cityName:'苏州',cityCode:'SU',dist:'常熟'},
{provinceName:'江苏',provinceCode:'JS',cityName:'苏州',cityCode:'SU',dist:'昆山'},
{provinceName:'江苏',provinceCode:'JS',cityName:'苏州',cityCode:'SU',dist:'太仓'},
{provinceName:'江苏',provinceCode:'JS',cityName:'南通',cityCode:'NT',dist:'南通'},
{provinceName:'江苏',provinceCode:'JS',cityName:'南通',cityCode:'NT',dist:'启东'},
{provinceName:'江苏',provinceCode:'JS',cityName:'南通',cityCode:'NT',dist:'海安'},
{provinceName:'江苏',provinceCode:'JS',cityName:'南通',cityCode:'NT',dist:'海门'},
{provinceName:'江苏',provinceCode:'JS',cityName:'徐州',cityCode:'XZ',dist:'徐州'},
{provinceName:'江苏',provinceCode:'JS',cityName:'徐州',cityCode:'XZ',dist:'沛县'},
{provinceName:'江苏',provinceCode:'JS',cityName:'徐州',cityCode:'XZ',dist:'龙固'},
{provinceName:'江苏',provinceCode:'JS',cityName:'徐州',cityCode:'XZ',dist:'贾汪'},
{provinceName:'江苏',provinceCode:'JS',cityName:'无锡',cityCode:'WX',dist:'无锡'},
{provinceName:'江苏',provinceCode:'JS',cityName:'无锡',cityCode:'WX',dist:'江阴'},
{provinceName:'江苏',provinceCode:'JS',cityName:'无锡',cityCode:'WX',dist:'宜兴'},
{provinceName:'江苏',provinceCode:'JS',cityName:'无锡',cityCode:'WX',dist:'长泾'},
{provinceName:'江苏',provinceCode:'JS',cityName:'无锡',cityCode:'WX',dist:'华士'},
{provinceName:'江苏',provinceCode:'JS',cityName:'无锡',cityCode:'WX',dist:'安镇'},
{provinceName:'江苏',provinceCode:'JS',cityName:'无锡',cityCode:'WX',dist:'祝塘'},
{provinceName:'江苏',provinceCode:'JS',cityName:'无锡',cityCode:'WX',dist:'丁蜀'},
{provinceName:'江苏',provinceCode:'JS',cityName:'常州',cityCode:'CH',dist:'常州'},
{provinceName:'江苏',provinceCode:'JS',cityName:'常州',cityCode:'CH',dist:'金坛'},
{provinceName:'江苏',provinceCode:'JS',cityName:'泰州',cityCode:'TZ',dist:'泰州'},
{provinceName:'江苏',provinceCode:'JS',cityName:'泰州',cityCode:'TZ',dist:'兴化'},
{provinceName:'江苏',provinceCode:'JS',cityName:'泰州',cityCode:'TZ',dist:'泰兴'},
{provinceName:'江苏',provinceCode:'JS',cityName:'泰州',cityCode:'TZ',dist:'靖江'},
{provinceName:'江苏',provinceCode:'JS',cityName:'扬州',cityCode:'YZ',dist:'扬州'},
{provinceName:'江苏',provinceCode:'JS',cityName:'扬州',cityCode:'YZ',dist:'江都'},
{provinceName:'江苏',provinceCode:'JS',cityName:'扬州',cityCode:'YZ',dist:'仪征'},
{provinceName:'江苏',provinceCode:'JS',cityName:'盐城',cityCode:'YC',dist:'盐城'},
{provinceName:'江苏',provinceCode:'JS',cityName:'连云港',cityCode:'LY',dist:'连云港'},
{provinceName:'江苏',provinceCode:'JS',cityName:'连云港',cityCode:'LY',dist:'赣榆'}
];


var g_can_debug = true;
function _debug() {
	if (!g_can_debug)
		return;
	if (typeof (console) != "undefined" && typeof (console.debug) != "undefined" ) {
		console.debug(arguments);
	}
}

function _log() {
	if (!g_can_debug)
		return;
	if (typeof (console) != "undefined") {
		console.log(arguments);
	}
}

var g_province = [];
var g_cities = [];
var g_dists = [];
var g_blank_html = "<option value=''>请选择</option>";
function initCitys() {
	_debug("initCitys start");
	$.each(g_all_dists, function(idx, item) {
		var needMe = true;
		for ( var i = 0; i < g_province.length; i++) {
			if (g_province[i].provinceCode == item.provinceCode) {
				needMe = false;
				break;
			}
		}
		if (needMe) {
			g_province.push(item);
		}
	});
	_debug("g_province:", g_province);

	$.each(g_all_dists, function(idx, item) {
		var needMe = true;
		for ( var i = 0; i < g_cities.length; i++) {
			if (g_cities[i].provinceCode == item.provinceCode
					&& g_cities[i].cityCode == item.cityCode) {
				needMe = false;
				break;
			}
		}
		if (needMe) {
			g_cities.push(item);
		}
	});
	_debug("g_cities:", g_cities);
	_debug("initCitys end");

	var dom_province = $("#province_id");
	var dom_city = $("#city_id");
	var dom_dist = $("#dist_id");

	var tempHtml = g_blank_html;
	$.each(g_province, function(idx, item) {
		tempHtml += "<option value='" + item.provinceCode + "'>"
				+ item.provinceName + "</option>";
	});
	dom_province.html(tempHtml);
	dom_province.change(function() {
		dom_dist.html(g_blank_html);
		if (dom_province.val() == "") {
			dom_city.html(g_blank_html);
			dom_city.trigger('change');
			return;
		}

		var tempHtml = ""; 
		var tempCount = 0;
		var tempVal = "";
		$.each(g_cities, function(idx, item) {
			if (item.provinceCode == dom_province.val()) {
				tempCount++;
				tempVal=item.cityCode;
				tempHtml += "<option value='" + item.cityCode + "'>"
						+ item.cityName + "</option>";
			}
		});
		if(tempCount>1){
			tempHtml = g_blank_html+tempHtml;
			dom_city.css('color','#333333');
		}else{
			dom_city.css('color','#999999');
		}
		
		dom_city.html(tempHtml);
		dom_city.trigger('change');
	});

	dom_city.change(function() {
		if (dom_city.val() == "") {
			dom_dist.html(g_blank_html);
			return;
		}

		var tempHtml = "";
		var tempCount = 0;
		var tempVal = "";
		$.each(g_all_dists, function(idx, item) {
			var optionIdx = 0;
			if (item.provinceCode == dom_province.val()
					&& item.cityCode == dom_city.val()) {
				tempCount++;
				tempVal=item.dist;
				if(item.dist=="南京市区" ||item.dist=="苏州市区" ||item.dist=="昆山"){
					//特例
				} else if(item.cityName != item.dist){
					tempVal = item.cityName + "-" + item.dist;
				}
				tempHtml += "<option  value='"
						+ tempVal + "'>" + item.dist + "</option>";
			}
		});
		if(tempCount>1){
			tempHtml = g_blank_html+tempHtml;
			dom_dist.css('color','#333333');
		}else{
			dom_dist.css('color','#999999');
		}
		
		dom_dist.html(tempHtml);
	});

}


